//
//  MBProgressHUD+Category.m
//  UtecPublicClass
//
//  Created by chijianguo on 2022/8/16.
//  Copyright © 2022 U-tec. All rights reserved.
//

#import "MBProgressHUD+Category.h"
#import "Public.h"
@implementation MBProgressHUD (Category)

+ (void)show:(NSString *_Nullable)text icon:(NSString *_Nullable)icon view:(UIView *_Nullable)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = @" ";

    hud.detailsLabel.font = FONT_BODY_16;
    hud.contentColor = UIColor.whiteColor;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])];
    hud.detailsLabel.attributedText = attributedString;
    [hud.detailsLabel sizeToFit];

    CGFloat textWidth = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName: FONT_BODY_16} context:nil].size.width;
        
    if (textWidth > (150)) {
        [hud.detailsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
             make.width.equalTo(@(150));
        }];
    }
    
    // 设置图片
    UIImageView *imageView =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    imageView.image = [UIImage imageNamed:icon];
    hud.customView = imageView;
    hud.square = YES;
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = COLOR_SIMPLEBLACK;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;

    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5];
}

+ (void)showSuccess:(NSString *_Nullable)success {
    [self showSuccess:success toView:nil];
}

+ (void)showSuccess:(NSString *_Nullable)success toView:(UIView *_Nullable)view {
    [self show:success icon:@"icon_success" view:view];
}

+ (void)showError:(NSString *_Nullable)error {
    [self showError:error toView:nil];
}

+ (void)showError:(NSString *_Nullable)error toView:(UIView *_Nullable)view {
    [self show:error icon:@"icon_error" view:view];
}

+ (MBProgressHUD *)showMessage:(NSString *_Nullable)message {
    return [self showMessage:message toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *_Nullable)message toView:(UIView *_Nullable)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.detailsLabel.text = message;
    hud.detailsLabel.font = FONT_BODY_16;
    hud.contentColor = rgb(70, 221, 162);
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = rgba(48, 48, 48, 0);
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    return hud;
}

- (void)cancelAction:(UIButton *)sender {
    [MBProgressHUD hideHUD];
}

+ (void)hideHUD {
    [self hideHUDForView:nil];
}

+ (void)hideHUDForView:(UIView *_Nullable)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    [self hideHUDForView:view animated:YES];
}

@end
